<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2015/12/17
 * Time: 14:41
 *  智慧夫子庙采用智杰闸机验证票付通订单
 */
include('/var/www/html/new/d/module/api/function.class.php');
if (!class_exists("go_sql")){
    include("/var/www/html/new/conf/le.je");
    $le=new go_sql();
    $le->connect();
}

//接收数据
$arr = $_REQUEST['BaseStr'];            //提交一串base过的数据赋值BaseStr传送。数据分用竖线‘|’分割两部分
//$arr = 'YjZkYWRhY2I4NGU3OTUxNWQwYTRiYzA1NWIxZGQ5NGF8eyJzYWxlcmlkIjoiNjAxMDc1IiwiY29kZSI6IjU5MDU4MCJ9';
$tmp = base64_decode($arr);
$datas  = explode('|',$tmp);
$sign = $datas[0];                       //前部分是账号+接口编码的MD5值，
$str  = json_decode($datas[1],true);    //后半部分是验证的信息，包含了景区salerid跟验证码code，并转换json格式提交

//景区所属判断
$salerid = $str['salerid'] + 0;
$pft_code  = $str['code'];
$salerid_arr = array('601075','863442','863443','863481','866682');
if(!in_array($salerid, $salerid_arr)) exit('{"code":"010A","msg":"景区无授权，拒绝连接"}');

$get_code = "SELECT l.apply_did,p.dcode,p.account FROM uu_land l left join pft_member p on l.apply_did = p.id where l.salerid='$salerid'  limit 1";
$GLOBALS['le']->query($get_code);
if(!$GLOBALS['le']->fetch_assoc()) exit('{"code":"010A","msg":"无此景区id，拒绝连接"}');  //景区salerid错误
$account = $GLOBALS['le']->f('account');
$apply_did = $GLOBALS['le']->f('apply_did');
$dcode = $GLOBALS['le']->f('dcode');

//密文校验
$checksign = md5($account.$dcode);
if($sign != $checksign) exit('{"code":"0005","msg":"密文校验失败"}');  //密文校验失败

//反查订单信息
$PFT_infunction = new AllFunction();
$selorder = $PFT_infunction->See_Code($salerid,$pft_code);
// echo $selorder;exit;
if(!$selorder) exit('{"code":"0122","msg":"订单未找到"}');  //密文校验失败

$pft_order_xml = $PFT_infunction->Select_PFTorder($selorder);
$chkIns = 499;
if((int)$pft_order_xml->Rec->UUstatus==2) $chkIns = 498;
$timestamp = date('Y-m-d H:i:s');
$r = $PFT_infunction->Unified_Check_IN($salerid,$pft_code,$timestamp,$chkIns);
if($r == 100){
    logfile($tmp.'|'.$selorder.'|'.$r,"fzmcheck_s");
    $response = array(
        'code'		=> '0000',
        'message'	=> '操作成功',
    );
    exit(json_encode($response));

}else{
    logfile($tmp.'|'.$selorder.'|'.$r,"fzmcheck_f");
    $response = array(
        'code'		=> Err_Code($r),
        'message'	=> Err_Word($r),
    );
    exit(json_encode($response));
}


function logfile($word,$path){
    $filepath= '/var/www/html/new/d/logs/api/yunding_check/'.$path.'.txt';
    $word = date("Y-m-d H:i:s")."|".$word;
    if(file_exists($filepath)){
        $maxsize  = 1024*1024*10;
        $filesize = filesize($filepath);
        $tmp = explode(".",$filepath);
        $filetype = end($tmp);
        $filename = substr($filepath,0,strpos($filepath,'.'));
        if($filesize>$maxsize)
            rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
    }
    $fp = fopen($filepath,"a");
    flock($fp, LOCK_EX);
    fwrite($fp,$word."\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}

//错误代码
function Err_Code($errcode){
    $arr_X_code=array(
        '101' => '0001',
        '290' => '0122',
        '519' => '0209',
        '288' => '0120',
        '293' => '0121',
        '521' => '0209',
        '105' => '0206',
        '203' => '010A',
        '255' => '0001',
    );
    $ecode=@$arr_X_code[$errcode];
    if (!$ecode) $ecode='0001';
    return $ecode;
}
//错误描述
function Err_Word($errcode){
    $arr_X_word=array(
        '101' => '服务器连接失败',
        '290' => '订单未找到',
        '519' => '游玩时间未到',
        '288' => '订单已过期',
        '293' => '订单已验证',
        '521' => '验证时间未到',
        '202' => '认证失败',
        '204' => '订单查找失败',
        '1077' => '订单已验证',
        '105' => '订单已取消',
        '203' => '未找到景区',
        '255' => '验证失败',
    );
    $eword=@$arr_X_word[$errcode];
    if (!$eword) $eword='其他错误';
    return $eword;
}
